package com.hctforgreen.greenservice.ui.widget;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.hctforgreen.greenservice.LearningActivity;
import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.TechnicalDataActivityV4;
import com.hctforgreen.greenservice.ctr.HctController;
import com.hctforgreen.greenservice.model.AttrEntity;
import com.hctforgreen.greenservice.model.SerieListEntity.ChildSerieEntity;
import com.hctforgreen.greenservice.model.SerieListEntity.SerieEntity;
import com.hctforgreen.greenservice.ui.adapter.LearningTypeAdapter;
import com.hctforgreen.greenservice.ui.adapter.SeriesLstAdapter;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.HctResult;

public class LearningTypeListPopupView extends PopupWindow {

	private View mConvertView;
	private Activity mActivity;

	private AttrEntity mCurrentSecondEntity;
	private LearningActivity mSelf;
	private int type;// 0:资料大类别 1:资料小类别

	private String pid;// 只用于获取小类别列表

	/**
	 * 
	 * @param activity
	 * @param currentSecondEntity
	 * @param self
	 * @param type
	 *            0:资料大类别 1:资料小类别
	 */
	public LearningTypeListPopupView(Activity activity,
			AttrEntity currentSecondEntity, LearningActivity self, int type,
			String pid) {
		super(activity);
		mActivity = activity;
		mCurrentSecondEntity = currentSecondEntity;
		mSelf = self;
		this.pid = pid;
		this.type = type;
		initWindow();
	}

	private void initWindow() {
		mConvertView = getLayout();

		this.setContentView(mConvertView);
		this.setBackgroundDrawable(new BitmapDrawable());
		this.setOutsideTouchable(true);
		this.setFocusable(true);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);

		loadDataLst();
	}

	private View getLayout() {
		View layout = mActivity.getLayoutInflater().inflate(
				R.layout.popup_series_list, null);
		return layout;
	}

	private void loadDataLst() {
		final LinearLayout loadLyt = (LinearLayout) mConvertView
				.findViewById(R.id.lyt_default_list_load);
		final LinearLayout reloadLyt = (LinearLayout) mConvertView
				.findViewById(R.id.lyt_default_list_reload);
		final ListView firstLv = (ListView) mConvertView
				.findViewById(R.id.lst_default_list);

		/**
		 * 最新学习资料
		 */
		final ListView recentListview = (ListView) mConvertView
				.findViewById(R.id.lst_default_list);

		final Button reloadBtn = (Button) mConvertView
				.findViewById(R.id.btn_default_list_reload);
		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {

				HctResult hctResult = new HctResult();
				switch (msg.what) {
				case HctConstants.STATUS_SUCCESS:

					loadLyt.setVisibility(View.GONE);
					reloadLyt.setVisibility(View.GONE);
					firstLv.setVisibility(View.VISIBLE);

					hctResult = (HctResult) msg.obj;

					List<AttrEntity> dataList = (ArrayList<AttrEntity>) hctResult.data;
					renderDataLst(firstLv, dataList);

					break;

				case HctConstants.STATUS_FAILD:
					loadLyt.setVisibility(View.GONE);
					reloadLyt.setVisibility(View.VISIBLE);
					firstLv.setVisibility(View.GONE);
					reloadBtn.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							loadDataLst();
						}
					});
					break;
				}
			}

		};
		loadLyt.setVisibility(View.VISIBLE);
		reloadLyt.setVisibility(View.GONE);
		new Thread() {
			public void run() {
				Message message = new Message();
				try {
					HctController controller = new HctController(mActivity);

					// HctResult result = controller
					// .getDbAllSeriesWithDbChildSeriesAndBooks(true);
					HctResult result = null;
					if (TextUtils.isEmpty(pid)) {// 获取大类别列表
						result = controller.getAllStudyType();
					} else {// 获取小类别列表
						result = controller.getChildTypeByTypeId(pid);
					}
					if (result.status == HctConstants.STATUS_SUCCESS) {
						message.what = result.status;
						message.obj = result;
					} else {
						message.what = HctConstants.STATUS_FAILD;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				handler.sendMessage(message);
			}
		}.start();
	}

	private void renderDataLst(ListView firstLv, List<AttrEntity> dataList) {

		new LearningTypeAdapter(firstLv, mActivity, dataList,
				mCurrentSecondEntity, LearningTypeListPopupView.this, type);

	}

	public void updateWindow(AttrEntity currentFirstEntity) {
		switch (type) {
		case 0:
			mSelf.initStudyTypeEntity(currentFirstEntity);
			mSelf.initStudyTypeBtn(currentFirstEntity);
			mSelf.clearChildTypeBtn();
			mSelf.setFocusable(false);
			break;
		case 1:
			mSelf.initStudyChildTypeEntity(currentFirstEntity);
			mSelf.initChildTypeBtn(currentFirstEntity);
			mSelf.setFocusable(false);
			break;
		default:
			break;
		}

	}
}
